<?php
/**
 * Created by IntelliJ IDEA.
 * User: andyhuang
 * Date: 2018/3/18
 * Time: 21:52
 */
require './vendor/autoload.php';
use Thenbsp\Wechat\OAuth\Client;

// define("APP_ID","wxa80b0b04be56b32a");
define("APP_ID","wxa4a788a854b99b34");
//define("APP_SECRET", "6878bcbc99bd84cfdc85cc652dec3fb8");
define("APP_SECRET", "7b9d71110b3e1755ad92fc42c61c79e2");


$path = '';

if(isset($_GET['path'])) {
    $path = $_GET['path'];
    // var_dump($user_url);
}

$client = new Client(APP_ID, APP_SECRET);

if( !isset($_GET['code']) ) {
    $client-> setState($path);
    header('Location: '.$client->getAuthorizeUrl());
}

if(isset($_GET['state'])){
    $path = $_GET['state'];
}

$accessToken = $client->getAccessToken($_GET['code']);

// var_dump($accessToken->toArray());

$openid = $accessToken->getOpenId();

$_SESSION['mdl_userid'] = $openid;
header('Location: '.$path);
exit;